﻿package com.gengine.core.frame
{
    import flash.events.*;
    import flash.utils.*;

    public class Frame extends Object implements IFrame
    {
        protected var _count:int;
        protected var _frameCount:int = 0;
        protected var _isPlay:Boolean;
        protected var delay:Number;
        private var _lastFrameCount:int = -1;
        protected var _oneFrameMap:Dictionary;
        protected var _otherFrameMap:Dictionary;
        private var _prevTime:int = 0;

        public function Frame(param1:Number)
        {
            this._isPlay = false;
            this.delay = param1;
            this._oneFrameMap = new Dictionary();
            this._otherFrameMap = new Dictionary();
            return;
        }// end function

        public function dispatchEvent() : void
        {
            return;
        }// end function

        public function play() : void
        {
            this._isPlay = true;
            return;
        }// end function

        public function stop() : void
        {
            this._isPlay = false;
            return;
        }// end function

        public function addTimer(param1:IBaseTimer) : void
        {
            if (!this._isPlay)
            {
                this.play();
            }
            if (param1.com.gengine.core.frame:IBaseTimer::delay == 1)
            {
                this._oneFrameMap[param1.id] = param1;
            }
            else
            {
                this._otherFrameMap[param1.id] = param1;
            }
            var _loc_2:String = this;
            var _loc_3:* = this._count + 1;
            _loc_2._count = _loc_3;
            return;
        }// end function

        public function removeTimer(param1:IBaseTimer) : void
        {
            if (this._oneFrameMap[param1.id])
            {
                delete this._oneFrameMap[param1.id];
            }
            else if (this._otherFrameMap[param1.id])
            {
                delete this._otherFrameMap[param1.id];
            }
            var _loc_2:String = this;
            var _loc_3:* = this._count - 1;
            _loc_2._count = _loc_3;
            return;
        }// end function

        protected function onEnterFrameHandler(event:Event = null) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = getTimer();
            if (this._prevTime == 0)
            {
                _loc_3 = 0;
            }
            else
            {
                _loc_3 = _loc_2 - this._prevTime;
                this.renderer(_loc_3);
            }
            this._prevTime = _loc_2;
            return;
        }// end function

        protected function renderer(param1:int, param2:Boolean = false) : void
        {
            var _loc_3:IBaseTimer = null;
            var _loc_4:String = this;
            var _loc_5:* = this._frameCount + 1;
            _loc_4._frameCount = _loc_5;
            for each (_loc_3 in this._oneFrameMap)
            {
                
                if (_loc_3.running)
                {
                    _loc_3.interval = param1;
                    _loc_3.renderer(this._frameCount, param2);
                    continue;
                }
                this.removeTimer(_loc_3);
            }
            for each (_loc_3 in this._otherFrameMap)
            {
                
                if (_loc_3.running)
                {
                    if (this._frameCount % _loc_3.delay == 0)
                    {
                        _loc_3.interval = param1;
                        _loc_3.renderer(this._frameCount, param2);
                    }
                    continue;
                }
                this.removeTimer(_loc_3);
            }
            return;
        }// end function

    }
}
